<template>
  <div id="travelactive" class="travel-active-container">
    <!-- 页面头部 -->
    <div class="header-section">
      <div class="header-content">
        <div class="title-section">
          <h2 class="page-title">
            <i class="el-icon-suitcase"></i> 跟团游活动列表
          </h2>
          <p class="page-subtitle">管理已通过的跟团游活动信息</p>
        </div>
        <div class="total-count">
          <div class="count-card">
            <span class="count-label">总活动数</span>
            <span class="count-value">{{ total }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索筛选区 -->
    <div class="search-filter-card">
      <div class="filter-group">
        <div class="filter-item">
          <label class="filter-label">
            <i class="el-icon-tickets"></i> 跟团游名称
          </label>
          <el-input
            v-model="filterParams.groupTourName"
            placeholder="输入跟团游名称搜索"
            clearable
            class="filter-input"
            @keyup.enter="handleFilter"
          />
        </div>
        <div class="filter-item">
          <label class="filter-label">
            <i class="el-icon-office-building"></i> 旅行社名称
          </label>
          <el-input
            v-model="filterParams.travelAgencyName"
            placeholder="输入旅行社名称搜索"
            clearable
            class="filter-input"
            @keyup.enter="handleFilter"
          />
        </div>
        <div class="filter-item">
          <label class="filter-label">
            <i class="el-icon-data-line"></i> 活动状态
          </label>
          <el-select
            v-model="filterParams.progressStatus"
            placeholder="选择活动状态"
            clearable
            class="filter-select"
          >
            <el-option label="未开始" value="未开始" />
            <el-option label="报名中" value="报名中" />
            <el-option label="报名截止" value="报名截止" />
            <el-option label="进行中" value="进行中" />
            <el-option label="已结束" value="已结束" />
          </el-select>
        </div>
      </div>
      
      <div class="action-buttons">
        <el-button 
          type="primary"  
          @click="handleFilter"
          class="search-btn"
        >
          查询
        </el-button>
        <el-button  
          @click="resetFilter"
          class="reset-btn"
        >
          重置
        </el-button>
      </div>
    </div>

    <!-- 数据表格 -->
    <div class="data-table-card">
      <el-table
        :data="tableData"
        border
        stripe
        highlight-current-row
        style="width: 100%"
        v-loading="loading"
        empty-text="暂无跟团游数据"
        :header-cell-style="{background: '#f5f7fa', color: '#606266'}"
      >
        <el-table-column 
          label="编号" 
          width="120" 
          align="center"
        >
          <template #default="scope">
            {{ getNaturalCode(scope.row.groupTourID) }}
          </template>
        </el-table-column>
        <el-table-column 
          prop="groupTourName" 
          label="跟团游名称" 
          width="220" 
          align="center"
        >
          <template #default="{ row }">
            <span class="tour-name">{{ row.groupTourName }}</span>
          </template>
        </el-table-column>
        <el-table-column 
          prop="travelAgencyName" 
          label="旅行社名称" 
          width="220" 
          align="center"
        >
          <template #default="{ row }">
            <span class="agency-name">{{ row.travelAgencyName }}</span>
          </template>
        </el-table-column>
        <el-table-column 
          prop="progressStatus" 
          label="活动状态" 
          width="160" 
          align="center"
        >
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.progressStatus)" effect="dark">
              {{ row.progressStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column 
          prop="recruitmentNumber" 
          label="招募人数" 
          width="140" 
          align="center"
        />
        <el-table-column 
          prop="registrationCount" 
          label="报名人数" 
          width="140" 
          align="center"
        />
        <el-table-column 
          label="操作" 
          width="120" 
          align="center"
          fixed="right"
        >
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleDetail(row.groupTourID)"
              plain
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页控制 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const tableData = ref([])
const loading = ref(false)
const total = ref(0)

// 筛选参数
const filterParams = ref({
  groupTourName: '',
  travelAgencyName: '',
  progressStatus: ''
})

// 分页参数
const pagination = ref({
  currentPage: 1,
  pageSize: 10
})

// 新增：用于缓存groupTourID与自然编号的映射
const idMap = new Map()
function getRandomCode() {
  return 'T' + Math.floor(Math.random() * 100000).toString().padStart(5, '0')
}
function getNaturalCode(groupTourID) {
  if (!idMap.has(groupTourID)) {
    let code
    do {
      code = getRandomCode()
    } while ([...idMap.values()].includes(code))
    idMap.set(groupTourID, code)
  }
  return idMap.get(groupTourID)
}

// 获取状态标签类型
const getStatusTagType = (status) => {
  const typeMap = {
    '未开始': 'info',
    '报名中': 'success',
    '报名截止': 'warning',
    '进行中': 'primary',
    '已结束': 'danger'
  }
  return typeMap[status] || ''
}

// 获取跟团游活动列表
const fetchData = async () => {
  try {
    loading.value = true
    
    // 调用后端API获取数据
    const response = await axios.get('/mw/GroupTourList')
    
    if (response.data && Array.isArray(response.data)) {
      // 应用前端筛选条件
      let filteredData = response.data.filter(item => {
        const matchesName = !filterParams.value.groupTourName || 
          item.groupTourName.includes(filterParams.value.groupTourName)
        const matchesAgency = !filterParams.value.travelAgencyName || 
          (item.travelAgencyName && item.travelAgencyName.includes(filterParams.value.travelAgencyName))
        const matchesStatus = !filterParams.value.progressStatus || 
          item.progressStatus === filterParams.value.progressStatus
        
        return matchesName && matchesAgency && matchesStatus
      })
      
      total.value = filteredData.length
      
      // 应用分页
      tableData.value = filteredData.slice(
        (pagination.value.currentPage - 1) * pagination.value.pageSize,
        pagination.value.currentPage * pagination.value.pageSize
      )
    }
  } catch (error) {
    ElMessage.error('获取跟团游数据失败: ' + (error.response?.data?.message || error.message))
  } finally {
    loading.value = false
  }
}

// 处理筛选
const handleFilter = () => {
  pagination.value.currentPage = 1
  fetchData()
}

// 重置筛选条件
const resetFilter = () => {
  filterParams.value = {
    groupTourName: '',
    travelAgencyName: '',
    progressStatus: ''
  }
  handleFilter()
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pagination.value.pageSize = val
  fetchData()
}

// 处理当前页变化
const handleCurrentChange = (val) => {
  pagination.value.currentPage = val
  fetchData()
}

// 查看详情
const handleDetail = (id) => {
  router.push(`/travel/active-detail/${id}`)
}

// 初始化加载数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped lang="scss">
/* 整体容器 */
.travel-active-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
  background-color: #f8fafc;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
}

/* 头部区域 */
.header-section {
  background: linear-gradient(135deg, #a0c4e4 0%, #7fa8d1 100%);
  padding: 20px 25px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(160, 196, 228, 0.2);
  margin-bottom: 24px;
  color: #2c3e50;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title-section {
  .page-title {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 10px;
    
    i {
      font-size: 28px;
      color: #2c3e50;
    }
  }
  
  .page-subtitle {
    margin: 5px 0 0;
    font-size: 14px;
    color: #4a5568;
  }
}

.total-count {
  .count-card {
    background: rgba(255, 255, 255, 0.7);
    padding: 12px 20px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 1px solid #e2e8f0;
    
    .count-label {
      font-size: 14px;
      margin-bottom: 5px;
      color: #4a5568;
    }
    
    .count-value {
      font-size: 24px;
      font-weight: 700;
      color: #2c3e50;
    }
  }
}

/* 搜索筛选卡片 */
.search-filter-card {
  background-color: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.filter-group {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.filter-item {
  display: flex;
  flex-direction: column;
  
  .filter-label {
    font-size: 14px;
    color: #606266;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 500;
    
    i {
      font-size: 16px;
    }
  }
  
  .filter-input {
    :deep(.el-input__inner) {
      border-radius: 8px;
      height: 40px;
      line-height: 40px;
    }
  }
  
  .filter-select {
    width: 100%;
    
    :deep(.el-input__inner) {
      border-radius: 8px;
      height: 40px;
      line-height: 40px;
    }
  }
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  
  .search-btn {
    background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    height: 40px;
    
    &:hover {
      opacity: 0.9;
    }
  }
  
  .reset-btn {
    border-radius: 8px;
    padding: 10px 20px;
    height: 40px;
    border: 1px solid #dcdfe6;
    
    &:hover {
      color: #4299e1;
      border-color: #c6e2ff;
      background-color: #ecf5ff;
    }
  }
}

/* 数据表格卡片 */
.data-table-card {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  margin-bottom: 20px;
  
  :deep(.el-table) {
    border-radius: 12px 12px 0 0;
    overflow: hidden;
    
    th {
      background-color: #f8fafc !important;
      font-weight: 600;
      color: #4a5568;
    }
    
    td {
      padding: 12px 0;
    }
    
    .el-table__row:hover {
      background-color: #f0f4f8;
    }
  }
}

.tour-name {
  font-weight: 500;
  color: #2c3e50;
}

.agency-name {
  font-weight: 500;
  color: #4299e1;
}

/* 分页样式 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 15px 0;
  
  :deep(.el-pagination) {
    .btn-prev, .btn-next {
      border-radius: 8px;
      border: 1px solid #e2e8f0;
    }
    
    .el-pager li {
      border-radius: 8px;
      border: 1px solid #e2e8f0;
      margin: 0 4px;
      background: white;
      
      &.active {
        background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
        color: white;
      }
    }
    
    .el-pagination__jump {
      .el-input__inner {
        border-radius: 8px;
      }
    }
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .filter-group {
    grid-template-columns: 1fr;
  }
  
  .action-buttons {
    justify-content: center;
  }
}
</style>

<style>
/* 全局样式覆盖 */
.success-message {
  border-radius: 8px !important;
  background: #f0f9eb !important;
  border: none !important;
}

.error-message {
  border-radius: 8px !important;
  background: #fef0f0 !important;
  border: none !important;
}
</style>